const Base = require('./base.js');

module.exports = class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  async listAction() {
    
    const model = this.model('tags');
    const data = await model.order(['count DESC']).page(1, 6).countSelect();

    return this.success(data);
  }

  async allAction() {

    const model = this.model('tags');
    const data = await model.order(['id DESC']).select()
    return this.success(data);
  }

  async putAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('tags');

    if (id > 0) {
      await model.where({id: id}).update(values);
    } else {
      delete values.id;

      const hasUser = await model.where({name: values.name}).find()
      if(!think.isEmpty(hasUser)){
        return this.fail(400, '名称已存在');
      }
      values.create_time = ['exp', 'CURRENT_TIMESTAMP()']

      await model.add(values);
    }
    return this.success(values);
  }

  async destoryAction() {
    const id = this.post('id');
    await this.model('user').where({id: id}).limit(1).delete();
    // TODO 删除图片

    return this.success();
  }
};
